package com.reesechou.controller.admin;

import com.reesechou.javabean.User;
import com.reesechou.system.Constants;
import com.reesechou.system.SystemConfig;
import com.reesechou.system.SystemInitServlet;
import com.sun.deploy.net.HttpRequest;
import org.directwebremoting.util.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by 小星打屁屁 on 2015/3/19.
 * contact me ==>243709758@qq.com
 * 登录控制器
 */
@Controller
@RequestMapping("/jsp")
public class LoginController {
  private  static Logger logger = Logger.getLogger(LoginController.class);
  private  static String LOGIN = "/jsp/login";
  private  static String LOGIN_SUCCESS = "admin/index";
  private  static String LOGIN_OUT = "/jsp/login";

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        logger.info("get login url");
        return LOGIN ;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletRequest request,HttpServletResponse response,User user){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String isRemember = request.getParameter("isRemember");
        if(StringUtils.hasText(isRemember)){
            //如果用户勾选记住我，则将用户名存入cookies
            Cookie cookie = new Cookie(Constants.COOKIE_IS_REMEMBER_USERNAME,username);//or user.getUserName
            cookie.setMaxAge(Constants.COOKIE_IS_REMEMBER_MAX_ACTIVE_TIME);//设置cookie为一周
            response.addCookie(cookie);
        }
        Map<String,String>  sc = new HashMap<String, String>();
        sc = SystemConfig.getSystemConfig();
        String rightName = sc.get("ExtendLoveJ.user.username");
        String rightPasswd = sc.get("ExtendLoveJ.user.password");
/*        if(StringUtils.hasText(username)&&username.equals(rightName) ){
             if(StringUtils.hasText(password)&&password.equals(rightPasswd)){
                 return LOGIN_SUCCESS ;
             }
            else {
                 logger.error("登录失败，验证不成功");
                 return LOGIN_OUT ;
             }
        } */

        return LOGIN_SUCCESS ;
    }


}
